package main

import (
	"context"
	"fmt"
	"os"

	"gitea.buaaica.org/ASI/kingbase_exporter/internal/scraper"
	"github.com/urfave/cli/v2"
)

var globalFlags = []*GlobalFlag{
	{
		firstLevelName:  "web",
		secondLevelName: "addr",
		usage:           "Address to listen on for web interface and telemetry.",
		defaultVal:      ":9144",
	},
	{
		firstLevelName:  "web",
		secondLevelName: "metrics-path",
		usage:           "Path under which to expose metrics.",
		defaultVal:      "/metrics",
	},
	{
		firstLevelName:  "web",
		secondLevelName: "timeout",
		usage:           "The timeout for query requests.",
		defaultVal:      10,
	},
	{
		firstLevelName:  "log",
		secondLevelName: "level",
		usage:           "Logging verbosity.",
		defaultVal:      "info",
	},
	{
		firstLevelName:  "config",
		secondLevelName: "path",
		usage:           "Path for config file which will be overwritten by the command line parameters.",
		defaultVal:      "",
	},
}

var defaultCollectorFlags = map[scraper.Scraper]bool{
	&scraper.ScrapeMemory{}: true,
	&scraper.ScrapeCPU{}:    true,
	&scraper.ScrapeDisk{}:   true,
	&scraper.ScrapeNet{}:    true,
}

func init() {
	addGlobalFlags()
	addCollectorFlags()
}

// entry for program which is a DI container
func main() {
	App.Action = func(ctx *cli.Context) error {
		conf := Conf{}
		if err := conf.Init(ctx); err != nil {
			fmt.Printf("[\033[1;31;40merror\033[0m] %v", err.Error())
			return err
		}

		if err := Run(context.Background(), &conf); err != nil {
			fmt.Printf("[\033[1;31;40merror\033[0m] %v", err.Error())
			return err
		}

		return nil
	}

	_ = App.Run(os.Args)
}
